from selenium import webdriver
from time import sleep
from PIL import Image
# from .api import main
from selenium.common.exceptions import NoSuchElementException
from .login import login_test
from .verification import main
import re
from selenium.webdriver.chrome.options import Options
from ..lib.screenshot_function import screenshot


def open_browser(browser_name, url):
    driver = None
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--window-size=1920,1080')

    if browser_name == "chrome":
        driver = webdriver.Chrome(chrome_options=chrome_options)
    if browser_name == "ff":
        driver = webdriver.Firefox()
    if browser_name == "ie":
        driver = webdriver.Ie()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    return driver


class TestKeys(object):

    def __init__(self, browser_name, url, username, password, new_password):
        self.driver = open_browser(browser_name, url)
        self.username = username
        self.password = password
        self.new_password = new_password

    def webui1(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 单位管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[1]').click()
        sleep(2)
        returns_list = []
        for dicts in list_dicts:
            print(driver)
            sleep(3)
            if dicts["unit_name"] and dicts["contact_name"] is not None:
                # 筛选条件-单位名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(
                    dicts["unit_name"])
                sleep(1)
                # 单位联系人
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').send_keys(
                    dicts["contact_name"])
                sleep(1)
                # 查询
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
                sleep(3)
                try:
                    unit_name_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/table/tbody/tr/td[2]/div').text
                    contact_name_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/table/tbody/tr/td[3]/div').text
                    if unit_name_text == dicts["unit_name"] and contact_name_text == dicts["contact_name"]:
                        error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                        print(error_text)
                        returns_list.append(error_text)
                        driver.refresh()
                        continue
                except Exception as a:
                    error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                    print(error_text)
                    returns_list.append(error_text)
                    driver.refresh()
                    continue
            else:
                if dicts["unit_name"] is not None:
                    # 筛选条件-单位名称
                    driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["unit_name"])
                    sleep(1)
                    # 查询
                    driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
                    sleep(3)
                    error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                    print(error_text)
                    if error_text == "":
                        error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                        print(error_text)
                    # # 重置
                    # driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[2]').click()
                    # sleep(1)
                    returns_list.append(error_text)
                    driver.refresh()
                    continue
                if dicts["contact_name"] is not None:
                    # 单位联系人
                    driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').send_keys(dicts["contact_name"])
                    sleep(1)
                    # 查询
                    driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
                    sleep(3)
                    error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
                    print(error_text)
                    if error_text == "":
                        error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
                        print(error_text)
                    # # 重置
                    # driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[2]').click()
                    # sleep(2)
                    returns_list.append(error_text)
                    driver.refresh()
                    continue
        # print(list_dicts)
        return returns_list

    def webui2(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 单位管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[1]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            driver.refresh()
            # 新增单位
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/header/div[2]/button[1]').click()
            sleep(2)
            if dicts["unit_name"] is not None:
                # 单位名称
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div[1]/input').send_keys(dicts["unit_name"])
                sleep(1)
            if dicts["unit_shortname"] is not None:
                # 单位简称
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/input').send_keys(dicts["unit_shortname"])
                sleep(1)
            if dicts["unit_coding"] is not None:
                # 单位编码
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div[1]/input').send_keys(dicts["unit_coding"])
                sleep(1)
            if dicts["unit_principal"] is not None:
                # 单位负责人
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div[1]/input').send_keys(dicts["unit_principal"])
                sleep(1)
            if dicts["phone_number"] is not None:
                # 联系电话
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[5]/div/div[1]/input').send_keys(dicts["phone_number"])
                sleep(1)
            # 保存
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/button[2]').click()
            sleep(2)
            if dicts["unit_name"] == dicts["unit_name_2"]:
                alert_text = driver.find_element_by_xpath('/html/body/div[3]/p').text
                print(alert_text)
                returns_list.append(alert_text)
                continue
            if dicts["unit_name"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            if dicts["unit_coding"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            if dicts["unit_principal"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            if dicts["phone_number"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[5]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            if not 2 <= len(dicts["unit_name"]) <= 32:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            if not 2 <= len(dicts["unit_shortname"]) <= 32:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            if not 2 <= len(dicts["unit_coding"]) <= 32:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            if not 2 <= len(dicts["unit_principal"]) <= 32:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            phone_pat = re.compile('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$')
            ret = re.search(phone_pat, dicts["phone_number"])
            print(ret)
            if ret:
                print("手机号码正确")
                pass
            else:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[5]/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                # driver.refresh()
                continue
            try:
                alert_text = driver.find_element_by_xpath('/html/body/div[6]/p').text
                print(alert_text)
                returns_list.append(alert_text)
                # driver.refresh()
                continue
            except Exception as a:
                # 单位名称
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["unit_name"])
                # 查询
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
                sleep(3)
                unit_name_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/table/tbody/tr/td[2]/div').text
                print(unit_name_text, dicts["unit_name"])
                if unit_name_text == dicts["unit_name"]:
                    returns_list.append("添加成功")
                    # driver.refresh()
                    continue
                else:
                    returns_list.append("添加失败")
                    # driver.refresh()
                    continue
        print(returns_list)
        return returns_list

    def webui3(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 单位管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[1]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            # 单位名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["unit_name"])
            sleep(1)
            # 单位联系人
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').send_keys(dicts["unit_principal"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
            sleep(2)
            # 编辑
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/table/tbody/tr/td[7]/div/button').click()
            sleep(1)
            # 单位名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div/input').clear()
            sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div/input').send_keys(dicts["unit_name_2"])
            sleep(1)
            # 单位简称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/input').clear()
            sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/input').send_keys(dicts["unit_shortname"])
            sleep(1)
            # 单位编码
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div/input').clear()
            sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div/input').send_keys(dicts["unit_coding"])
            sleep(1)
            # 单位负责人
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div/input').clear()
            sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div/input').send_keys(dicts["unit_principal_2"])
            sleep(1)
            # 联系电话
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[5]/div/div/input').clear()
            sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[5]/div/div/input').send_keys(dicts["phone_number"])
            sleep(1)
            # 排序号
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[10]/div/div/input').clear()
            sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[10]/div/div/input').send_keys(dicts["serial_number"])
            sleep(1)
            # 保存
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/div[1]/button[2]').click()
            sleep(2)
            # 单位名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').clear()
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["unit_name_2"])
            sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').clear()
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').send_keys(dicts["unit_principal_2"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
            sleep(5)
            error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/footer/div/div/slot/span').text
            print(error_text)
            if error_text == "共 1 条":
                returns_list.append("编辑成功")
            else:
                returns_list.append("编辑失败")
            # 重置
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[2]').click()
            sleep(1)
            continue
        print(returns_list)
        return returns_list

    def webui4(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 单位管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[1]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            # 单位名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["unit_name"])
            sleep(1)
            # 单位联系人
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').send_keys(dicts["unit_principal"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
            sleep(2)
            # 选中
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[2]/table/thead/tr/th[1]/div/label/span').click()
            sleep(1)
            # 批量删除
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/header/div[2]/button[2]').click()
            sleep(1)
            # 确定
            driver.find_element_by_xpath('/html/body/div[5]/div/div[3]/button[2]').click()
            sleep(2)
            # 单位名称
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').clear()
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(
                dicts["unit_name"])
            sleep(1)
            # 单位联系人
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').clear()
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/div/div/div/input').send_keys(
                dicts["unit_principal"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[1]').click()
            sleep(2)
            error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/div/div[3]/div/span').text
            print(error_text)
            if error_text == "暂无数据":
                returns_list.append("删除成功")
            else:
                returns_list.append("删除失败")
            # 重置
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[3]/button[2]').click()
            sleep(1)
            continue
        print(returns_list)
        return returns_list

    def quit(self):
        """
        浏览器退出
        :return:
        """
        self.driver.quit()
